package com.library.dcy.service;


import com.library.dcy.dao.BookDaoImpl;
import com.library.dcy.entity.BorrowInfo;
import com.library.dcy.entity.PageInfo;
import com.library.dcy.entity.RepayInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookServiceImpl implements BookService {
    BookDaoImpl bookDao=new BookDaoImpl();
    /**
     * 按读者id分页查询借书记录
     * @param id//读者id
     * @param currentPage//当前页码
     * @return
     */
    @Override
    public PageInfo<BorrowInfo> borrow(Integer id, Integer currentPage) {
        //新建分页对象
        PageInfo<BorrowInfo> pageInfo=new PageInfo<>();
        //设置每页的显示行数
        pageInfo.setPageSize(5);
        //设置当前页码
        pageInfo.setCurrentPage(currentPage);
        //获取总记录数调用dao层去查询
        Integer count = null;
        try {
            count = borrowcount(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //设置总记录数
        pageInfo.setTotalCount(count);
        //根据当前页查出起始下标
        Integer star=(pageInfo.getCurrentPage()-1)*pageInfo.getPageSize();
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("start",star);
        map.put("pageSize",pageInfo.getPageSize());
        //调用dao查询当前页的数据的集合
        List<BorrowInfo> pagborrow = null;
        try {
            pagborrow = bookDao.borrow(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //存入分页对象
        pageInfo.setList(pagborrow);
        //将分页对象返回
        return pageInfo;
    }

    /**
     * 分页查询借书总记录数
     * @param id//读者id
     * @return
     */
    @Override
    public Integer borrowcount(Integer id) throws Exception {
        return bookDao.borrowcount(id);
    }

    /**
     * 按读者id分页查询还书记录
     * @param id//读者id
     * @param currentPage//当前页码
     * @return
     */
    @Override
    public PageInfo<RepayInfo> repay(Integer id, Integer currentPage) {
        //新建分页对象
        PageInfo<RepayInfo> pageInfo=new PageInfo<>();
        //设置每页的显示行数
        pageInfo.setPageSize(5);
        //设置当前页码
        pageInfo.setCurrentPage(currentPage);
        //获取总记录数调用dao层去查询
        Integer count = null;
        try {
            count = repaycount(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //设置总记录数
        pageInfo.setTotalCount(count);
        //根据当前页查出起始下标
        Integer star=(pageInfo.getCurrentPage()-1)*pageInfo.getPageSize();
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("start",star);
        map.put("pageSize",pageInfo.getPageSize());
        //调用dao查询当前页的数据的集合
        List<RepayInfo> pagrepay = null;
        try {
            pagrepay = bookDao.repay(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //存入分页对象
        pageInfo.setList(pagrepay);
        //将分页对象返回
        return pageInfo;
    }

    /**
     * 分页查询还书总记录数
     * @param id//读者id
     * @return
     */
    @Override
    public Integer repaycount(Integer id) throws Exception {
        return bookDao.repaycount(id);
    }
}
